package com.example.demo.controller;

import com.example.demo.controller.requests.ModifyOrderRequest;
import com.example.demo.controller.requests.NewOrderRequest;
import com.example.demo.model.Order;
import com.example.demo.service.IDrinkService;
import com.example.demo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IDrinkService drinkService;

    @PostMapping("/")
    public Order addNewOrder(@RequestBody NewOrderRequest request){
        Order order=new Order();
        BeanUtils.copyProperties(request,order);
        orderService.addOrder(order,request.getItems());
        return order;
    }
    @PutMapping("/")
    public Order modifOrder(ModifyOrderRequest request) {
        log.info("修改的订单状态:{}", request);
        //同通过id查询出订单
        Order order = orderService.getOrderById(request.getId());
        //复制出属性
        BeanUtils.copyProperties(request, order);
        //更新数据
        orderService.updataOrder(order);
        return order;
    }

    @DeleteMapping("/{id}")
    public  String deleteOrder(@PathVariable Integer id){
        orderService.deleteOrderByid(id);
        log.info("删除的id为:{}", id);
        return "删除操作成功";
    }

    //get /order/张三
    @GetMapping("/{customer}")
    public List<Order> getOrdersByCustomer(@PathVariable String customer){
        return orderService.getOrdersByCustomer(customer);
    }
}
